var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:{},
    money:0,
    log:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.user_info(app.globalData.userInfo.id)
  },

  money_action(e){
    this.setData({
      money:e.detail.value
    })
  },

  recharge(){
    var that = this
    if(this.data.money == 0){
      wx.showToast({
        title: '请输入充值金额',
      })
      return;
    }
    if(this.data.money <= 0){
      wx.showModal({
        content: '充值金额不能小于0',
      })
      return;
    }
    wx.request({
      url: getApp().globalData.httpUrl+"api/device/charge",
      header: {
        'content-type': 'application/x-www-form-urlencoded','appid':getApp().globalData.appId
      },
      method: 'POST',
      dataType: 'json',
      data:{user_id:app.globalData.userInfo.id,money:that.data.money},
      success: function(t) {
        wx.requestPayment({
          timeStamp: t.data.timeStamp,
          nonceStr: t.data.nonceStr,
          package: t.data.package,
          signType: t.data.signType,
          paySign: t.data.paySign,
          success: function(t) {},
          complete: function(t) {                     
              console.log(t), "requestPayment:fail cancel" == t.errMsg && wx.showToast({
                  title: "取消支付",
                  icon: "loading",
                  duration: 1e3
              }), "requestPayment:ok" == t.errMsg && (
                  wx.showToast({
                    title: "支付成功",
                  })           
              );
              
              if(t.errMsg=="requestPayment:ok"){
                setTimeout(function() {
                  wx.switchTab({
                    url: '/pages/mine/mine',
                  })
                }, 2000)     
              }
              
          }
        });
      }
    })
  },

  //用户
  user_info:function(id){
    var that = this;
    wx.request({
      url: app.globalData.httpUrl+"api/mini/user_info",
      header: {
          'content-type': 'application/x-www-form-urlencoded','appid':app.globalData.appId
      },
      method: 'POST',
      data:{'id':id},
      dataType: 'json',
      success: function (res) {
          app.globalData.userInfo = res.data;
          that.setData({
            userInfo:app.globalData.userInfo
          })
      }
    })

    wx.request({
      url: getApp().globalData.httpUrl+"api/mini/amount_log",
      header: {
        'content-type': 'application/x-www-form-urlencoded','appid':getApp().globalData.appId
      },
      method: 'POST',
      dataType: 'json',
      data:{user_id:app.globalData.userInfo.id},
      success: function(t) {
        console.log(t)
        that.setData({
          log:t.data
        })
      }
    })


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.user_info(app.globalData.userInfo.id)

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})